import jdk.internal.jimage.ImageReader;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import java.util.Scanner;
import java.util.Vector;

public class GameJPanel extends JPanel implements KeyListener,Runnable {
    OurTank ourtank=new OurTank(100,100);
    Vector<EnemyTank> enemyTankVector=new Vector<>();
    Vector<EnemyTank> enemyTankVector1=new Vector<>();
    Scanner scanner=new Scanner(System.in);
    int flag=0;
    Random random=new Random();

    @Override
    public void paint(Graphics g) {
        super.paint(g);
//        g.setColor(Color.DARK_GRAY);
        g.fillRect(0, 0, 700, 500);
        drawTank(ourtank.getX(), ourtank.getY(), g, ourtank.getDirection(), ourtank.getType());
        for (int i = 0; i < 3; i++) {
            if(enemyTankVector.size()<3){
                int x=random.nextInt(500)+100;
                int y=random.nextInt(300)+50;
                EnemyTank enemytank = new EnemyTank(x, y);
                enemytank.setDirection(0);
                Thread thread = new Thread(enemytank);
                thread.start();
                enemyTankVector.add(enemytank);
            }
            drawTank(enemyTankVector.get(i).getX(), enemyTankVector.get(i).getY(), g, enemyTankVector.get(i).getDirection(), enemyTankVector.get(i).getType());
        }
        for(int i=0;i<enemyTankVector1.size() || flag==1;i++){
            if(flag==1){
                int x=random.nextInt(500)+100;
                int y=random.nextInt(300)+50;
                int dir=random.nextInt(4);
                EnemyTank enemytank = new EnemyTank(x, y);
                enemytank.setDirection(dir);
                Thread thread = new Thread(enemytank);
                thread.start();
                enemyTankVector1.add(enemytank);
                flag=0;
            }
            drawTank(enemyTankVector1.get(i).getX(), enemyTankVector1.get(i).getY(), g, enemyTankVector1.get(i).getDirection(), 2);
        }
        for (int i = 0; i < ourtank.bulletVector1.size(); i++) {
            Bullet bullet = ourtank.bulletVector1.get(i);
            if (bullet.isLife()) {
                g.setColor(Color.WHITE);
                g.fill3DRect(bullet.getX(), bullet.getY(), 3, 3, false);
            }else{
                ourtank.bulletVector1.remove(bullet);
            }
            for(int j=0;j<enemyTankVector.size();j++){
                EnemyTank enemytank =enemyTankVector.get(j);
                judge(bullet,enemytank);
                if(enemytank.isLife()==false) {
                    enemyTankVector.remove(enemytank);
                }
            }
            for(int j=0;j<enemyTankVector1.size();j++){
                EnemyTank enemytank =enemyTankVector1.get(j);
                judge1(bullet,enemytank);
                if(enemytank.isLife()==false) {
                    enemyTankVector1.remove(enemytank);
                }
            }
            if (!check(bullet)) {
                bullet.setLife(false);
            }
        }
    }
    public void drawTank(int x,int y,Graphics g,int direction,int type){
        //敌方：黄色type=0
        //我方：绿色type=1
        if(type==0){
            g.setColor(Color.GREEN);
        }else if(type==1){
            g.setColor(Color.YELLOW);
        }else if(type==2){
            g.setColor(Color.darkGray);
        }
        switch (direction){
            case 0://up
                g.fill3DRect(x,y,5,30,false);
                g.fill3DRect(x+15,y,5,30,false);
                g.fill3DRect(x+5,y+5,10,20,false);
                if(type==2){
                    g.setColor(Color.darkGray);
                }else{
                    g.setColor(Color.BLUE);
                }
                g.fillOval(x+5,y+7,5,5);
                g.fillOval(x+8,y+7,5,5);
                g.drawLine(x+7,y+7,x+7,y-3);
                g.drawLine(x+10,y+7,x+10,y-3);
                break;
            case 1://left
                g.fill3DRect(x,y,30,5,false);
                g.fill3DRect(x,y+15,30,5,false);
                g.fill3DRect(x+5,y+5,20,10,false);
                if(type==2){
                    g.setColor(Color.darkGray);
                }else{
                    g.setColor(Color.BLUE);
                }
                g.fillOval(x+7,y+5,5,5);
                g.fillOval(x+7,y+8,5,5);
                g.drawLine(x+7,y+7,x-3,y+7);
                g.drawLine(x+7,y+10,x-3,y+10);
                break;
            case 2://right
                g.fill3DRect(x,y,30,5,false);
                g.fill3DRect(x,y+15,30,5,false);
                g.fill3DRect(x+5,y+5,20,10,false);
                if(type==2){
                    g.setColor(Color.darkGray);
                }else{
                    g.setColor(Color.BLUE);
                }
                g.fillOval(x+18,y+5,5,5);
                g.fillOval(x+18,y+8,5,5);
                g.drawLine(x+23,y+7,x+33,y+7);
                g.drawLine(x+23,y+10 ,x+33,y+10);
                break;
            case 3://down
                g.fill3DRect(x,y,5,30,false);
                g.fill3DRect(x+15,y,5,30,false);
                g.fill3DRect(x+5,y+5,10,20,false);
                if(type==2){
                    g.setColor(Color.darkGray);
                }else{
                    g.setColor(Color.BLUE);
                }
                g.fillOval(x+5,y+18,5,5);
                g.fillOval(x+8,y+18,5,5);
                g.drawLine(x+7,y+18,x+7,y+32);
                g.drawLine(x+10,y+18 ,x+10,y+32);
                break;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }
    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_W){
            ourtank.setDirection(0);
            ourtank.up();
            if(ourtank.getY()<0) ourtank.setY(0);
        }else if(e.getKeyCode()==KeyEvent.VK_A){
            ourtank.setDirection(1);
            ourtank.left();
            if(ourtank.getX()<0) ourtank.setX(0);
        }else if(e.getKeyCode()==KeyEvent.VK_D){
            ourtank.setDirection(2);
            ourtank.right();
            if(ourtank.getX()>700-30) ourtank.setX(700-30);
        }else if(e.getKeyCode()==KeyEvent.VK_S){
            ourtank.setDirection(3);
            ourtank.down();
            if(ourtank.getY()>500-30) ourtank.setY(500-30);
        }
        if(e.getKeyCode()==KeyEvent.VK_J){
            if(ourtank.bulletVector1.size()<5){
                ourtank.attack();
            }
        }
        if(e.getKeyCode()==KeyEvent.VK_1){
            for(int i=0;i<1000;i++){
                int x=random.nextInt(700);
                int y=random.nextInt(500);
                for(int j=0;j<enemyTankVector.size();j++){
                    EnemyTank enemyTank=enemyTankVector.get(j);
                    if(enemyTank.getX()<x && enemyTank.getX()+25>x && enemyTank.getY()<y && enemyTank.getY()+25>y && enemyTank.isLife()==true){
                        enemyTank.setLife(false);
                        System.out.println("==========================");
                    }
                }
                System.out.println(x +"   " + y);
            }
        }
        if(e.getKeyCode()==KeyEvent.VK_2){
            flag=1;
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void run() {
        while(true){
            try{
                Thread.sleep(50);
            } catch(InterruptedException e){
                throw new RuntimeException(e);
            }
            this.repaint();
        }
    }
    public boolean check(Bullet bullet){
        if(bullet.getX()<0 || bullet.getX()>700 || bullet.getY()<0 || bullet.getY()>500){
            return false;
        }
        return true;
    }
    public void judge(Bullet bullet,EnemyTank enemyTank){
        if(bullet.getX()>enemyTank.getX()
                && bullet.getY()>enemyTank.getY()
                && bullet.getX()<enemyTank.getX()+25
                && bullet.getY()<enemyTank.getY()+25){
                bullet.setLife(false);
                enemyTank.setLife(false);
        }
    }
    public void judge1(Bullet bullet,EnemyTank enemyTank){
        if(bullet.getX()>enemyTank.getX()
                && bullet.getY()>enemyTank.getY()
                && bullet.getX()<enemyTank.getX()+25
                && bullet.getY()<enemyTank.getY()+25){
            bullet.setLife(false);
            enemyTank.setLife(false);
        }
    }
}